package com.woniuxy.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


/**
 * 全局日志输出组件
 */
@Slf4j
@Component
@Aspect
public class GlobalLogOutPut {

    /**
     * 输出日志
     * @param proceedingJoinPoint 准备执行的方法的JoinPoint对象
     * @return 方法的返回值
     */
    @Around(value = "execution(* com..controller..*Controller.*(..))")
    public Object outputLog(ProceedingJoinPoint proceedingJoinPoint) {
        Object[] args =  proceedingJoinPoint.getArgs();
        Object proceed = null;
        try {
            // 获取方法所在的包
            String packageName = proceedingJoinPoint.getSignature().getDeclaringTypeName();
            String methodName = proceedingJoinPoint.getSignature().getName();
            log.info("进入 {} 类,执行了方法 {},接收到的参数为 {}", packageName, methodName + "()", args);
            proceed = proceedingJoinPoint.proceed();
            log.info("在 {},执行 {} 结束", packageName, methodName + "()");
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return proceed;
    }
}
